#include "message.h"

Message::Message(class AbstractNode * operand, Id * name) : AbstractNode()
{
	c_operand=operand;
	names.push_back(name);
}

Message::Message(class AbstractNode * operand, Id * name, class AbstractNode * param) : AbstractNode()
{
	c_operand=operand;
	names.push_back(name);
	params.push_back(param);
}

void Message::add(Id * name, class AbstractNode * param)
{
	if (!params.empty() && !names.empty()) {
		names.push_back(name);
		params.push_back(param);
	}
}

void Message::write(GraphStream * s)
{
	this->accept(s);

	fprintf(s->stream,"\tnode_%d [label=\"Message node\\n%s\" shape=\"rectangle\"]\n", this->number(),names.front()->identiefer()->c_str());
	c_operand->write(s);
	fprintf(s->stream,"\tnode_%d -> node_%d [label=\"operand\"]\n",this->number(),c_operand->number());
	if (!params.empty()){
		for (unsigned int i=0; i<params.size(); ++i){
			params[i]->write(s);
			fprintf(s->stream,"\tnode_%d -> node_%d [label=\"%s\"]\n",this->number(),params[i]->number(),names[i]->identiefer()->c_str());
		}
	}
}

Message::~Message()
{
	for (unsigned int i=0; i<params.size(); ++i) {
		delete params[i];	
	}
	for (unsigned int i=0; i<names.size(); ++i) {
		delete names[i];	
	}
}